package com.magpiebridge.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
public class UploadUtil {
    //oss域名
    public static final String ALI_DOMAIN = "https://photo-upload-magpiebridge.oss-cn-beijing.aliyuncs.com/";
    public static String uploadImages(MultipartFile file) throws IOException {
        //生成文件名
        String originalFilename = file.getOriginalFilename(); //原来的图片名
        String exit = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + exit;
        //地域节点
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tL7twj5ZTnYRafudGg8";
        String accessKeySecret = "gYvPMwrqTLDHZ5PEvjGjroCstbUz0C";
        //oss客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject(
                "photo-upload-magpiebridge", //仓库名
                fileName,  //文件名
                file.getInputStream()
        );
        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }
}
